/*
 * Copyright (C) 2016 Nishant Srivastava
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.nisrulz.sensey;

import ohos.sensor.bean.CategoryBody;
import ohos.sensor.bean.CategoryEnvironment;
import ohos.sensor.bean.CategoryLight;
import ohos.sensor.bean.CategoryMotion;
import ohos.sensor.bean.CategoryOrientation;
import ohos.sensor.bean.CategoryOther;
import ohos.sensor.data.CategoryBodyData;
import ohos.sensor.data.CategoryEnvironmentData;
import ohos.sensor.data.CategoryLightData;
import ohos.sensor.data.CategoryMotionData;
import ohos.sensor.data.CategoryOrientationData;
import ohos.sensor.data.CategoryOtherData;
import ohos.sensor.data.SensorData;
import ohos.sensor.listener.ICategoryBodyDataCallback;
import ohos.sensor.listener.ICategoryEnvironmentDataCallback;
import ohos.sensor.listener.ICategoryLightDataCallback;
import ohos.sensor.listener.ICategoryMotionDataCallback;
import ohos.sensor.listener.ICategoryOrientationDataCallback;
import ohos.sensor.listener.ICategoryOtherDataCallback;
import ohos.sensor.listener.ISensorDataCallback;

import java.util.ArrayList;

/**
 * 数据封装
 *
 * @since 2021-04-12
 */
abstract class SensorDetector<T extends ISensorDataCallback> {
    private final SensorType[] sensorTypes;
    private ArrayList<T> callbacks;

    SensorDetector(SensorType... sensorTypes) {
        this.sensorTypes = sensorTypes;
        createDataCallback();
    }

    SensorType[] getSensorTypes() {
        return sensorTypes;
    }

    ArrayList<T> getCallbacks() {
        return callbacks;
    }

    private void createDataCallback() {
        callbacks = new ArrayList<>();
        for (SensorType sensorType : sensorTypes) {
            int type = sensorType.getValue();
            if (type < SensorManager.CATEGORY_BODY) {
                callbacks.add((T) getBodyDataCallback());
            } else if (type < SensorManager.CATEGORY_ENVIRONMENT) {
                callbacks.add((T) getEnvironmentDataCallback());
            } else if (type < SensorManager.CATEGORY_LIGHT) {
                callbacks.add((T) getLightDataCallback());
            } else if (type < SensorManager.CATEGORY_MOTION) {
                callbacks.add((T) getMotionDataCallback());
            } else if (type < SensorManager.CATEGORY_ORIENTATION) {
                callbacks.add((T) getOrientationDataCallback());
            } else if (type < SensorManager.CATEGORY_OTHER) {
                callbacks.add((T) getOtherDataCallback());
            }
        }
    }

    void onSensorEvent(SensorData sensorData) {
    }

    private ICategoryOtherDataCallback getOtherDataCallback() {
        ICategoryOtherDataCallback callback = new ICategoryOtherDataCallback() {
            @Override
            public void onSensorDataModified(CategoryOtherData categoryOtherData) {
                onSensorEvent(categoryOtherData);
            }

            @Override
            public void onAccuracyDataModified(CategoryOther categoryOther, int i) {
            }

            @Override
            public void onCommandCompleted(CategoryOther categoryOther) {
            }
        };
        return callback;
    }

    private ICategoryOrientationDataCallback getOrientationDataCallback() {
        ICategoryOrientationDataCallback callback = new ICategoryOrientationDataCallback() {
            @Override
            public void onSensorDataModified(CategoryOrientationData categoryOrientationData) {
                onSensorEvent(categoryOrientationData);
            }

            @Override
            public void onAccuracyDataModified(CategoryOrientation categoryOrientation, int i) {
            }

            @Override
            public void onCommandCompleted(CategoryOrientation categoryOrientation) {
            }
        };
        return callback;
    }

    private ICategoryBodyDataCallback getBodyDataCallback() {
        ICategoryBodyDataCallback callback = new ICategoryBodyDataCallback() {
            @Override
            public void onSensorDataModified(CategoryBodyData categoryBodyData) {
                onSensorEvent(categoryBodyData);
            }

            @Override
            public void onAccuracyDataModified(CategoryBody categoryBody, int i) {
            }

            @Override
            public void onCommandCompleted(CategoryBody categoryBody) {
            }
        };
        return callback;
    }

    private ICategoryEnvironmentDataCallback getEnvironmentDataCallback() {
        ICategoryEnvironmentDataCallback callback = new ICategoryEnvironmentDataCallback() {
            @Override
            public void onSensorDataModified(CategoryEnvironmentData categoryEnvironmentData) {
                onSensorEvent(categoryEnvironmentData);
            }

            @Override
            public void onAccuracyDataModified(CategoryEnvironment categoryEnvironment, int i) {
            }

            @Override
            public void onCommandCompleted(CategoryEnvironment categoryEnvironment) {
            }
        };
        return callback;
    }

    private ICategoryMotionDataCallback getMotionDataCallback() {
        ICategoryMotionDataCallback callback = new ICategoryMotionDataCallback() {
            @Override
            public void onSensorDataModified(CategoryMotionData categoryMotionData) {
                onSensorEvent(categoryMotionData);
            }

            @Override
            public void onAccuracyDataModified(CategoryMotion categoryMotion, int i) {
            }

            @Override
            public void onCommandCompleted(CategoryMotion categoryMotion) {
            }
        };
        return callback;
    }

    private ICategoryLightDataCallback getLightDataCallback() {
        ICategoryLightDataCallback callback = new ICategoryLightDataCallback() {
            @Override
            public void onSensorDataModified(CategoryLightData categoryLightData) {
                onSensorEvent(categoryLightData);
            }

            @Override
            public void onAccuracyDataModified(CategoryLight categoryLight, int i) {
            }

            @Override
            public void onCommandCompleted(CategoryLight categoryLight) {
            }
        };
        return callback;
    }
}
